import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

// 解决：Uncaught (in promise) NavigationDuplicated
const originalPush = Router.prototype.push
const originalReplace = Router.prototype.replace

Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(e => e)
}

Router.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch(e => e)
}

/* Router Modules */
import redirect from '@/router/modules/redirect'
import login from '@/router/modules/login'
import authRedirect from '@/router/modules/authRedirect'
import { error401, error404 } from '@/router/modules/errorPage'
// 面板
import bashBoard from '@/router/modules/bashboard'
// 发货申请表
import applicationForm from '@/router/modules/applicationForm'
// 回款池
import cashBackPoll from '@/router/modules/cashBackPoll'
// 核销记录
import writeOffRecord from '@/router/modules/writeOffRecord'
// 账户
import clientAccountManagement from '@/router/modules/clientAccountManagement'
// 手动充值
import manualRecharge from '@/router/modules/manualRecharge'
// 主体管理
import topicManagement from '@/router/modules/topicManagement'
// 汇率设置
import exchangeSetting from '@/router/modules/exchangeSetting'
// 汇率设置
import remittance from '@/router/modules/remittance'

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  error404,
  error401,
  bashBoard,
  redirect,
  login,
  authRedirect
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
// 请勿重复使用 单词！！！
// 请勿重复使用 单词！！！
// 请勿重复使用 单词！！！
// 列如 pi 和 piList 这种piList包含pi
export const asyncRoutes = [
  ...[applicationForm],
  ...[remittance],
  ...[cashBackPoll],
  ...[writeOffRecord],
  ...[clientAccountManagement],
  ...[manualRecharge],
  ...[topicManagement],
  ...[exchangeSetting]
]

const createRouter = () => new Router({
  base: window.__POWERED_BY_QIANKUN__ ? '/fina-admin/' : '/',
  mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
